package bgate.connector;


import java.util.Vector;

//import custom.item.TextListItem;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;


public class DataTransfer  {
       
	private static DataTransfer dataTransfer;
	private static final String TAG="DataTransfer";
	private static final long serialVersionUID = 1L;
	public ThreadPool threadPool;
    public WriterThread writeThread = null;
    private MasterConnector masterClient = null;
    private ClientConnector clientConnector = null;
    public Vector outQueue = new Vector();
    private String host;
    private int port;
    private boolean isConnected;

	private DataTransfer(String host, int port) {
		Log.d("connect", host);
		this.host = host;
		this.port = port;
		Log.d(TAG, "new datatranfer");
		threadPool = new ThreadPool();
		masterClient = new MasterConnector(host, port, this, outQueue);

	}

	public static synchronized DataTransfer getInstance() {
		// DataTransfer("202.92.4.23",2039);
		if (dataTransfer == null) {
			dataTransfer = new DataTransfer("118.69.205.226", 8888);
//			dataTransfer = new DataTransfer("yome.vn", 8888);
//			dataTransfer = new DataTransfer("10.0.2.2", 8888);
		}
		return dataTransfer;
	}

    public void close()
    {
    	this.masterClient.stopConn2Master();
    }
    
	public boolean connect() {

		Log.d(TAG, "start connect to server");
		 clientConnector = masterClient.startConn2Slave(clientConnector);
		 if(clientConnector==null)
			 return false;
		 else return true;
		/*try {
			isConnected= masterClient.getSlaveServer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		 Log.d(TAG, "connect "+e.toString());
		 return false;
		}
		Log.d(TAG, "start connect to client");
		if(isConnected)
		{
		    clientConnector = masterClient.startConn2Slave(clientConnector);
		    return true;
		}
		return false;*/

	}
        
    public Vector getOutQueue(){
        return outQueue;
    }
    
    public void removeTask(Runnable runnable){
    	outQueue.remove(runnable);
    	threadPool.resetTask(runnable);
    }
    
    
    public void putTask(Runnable runnable){
        threadPool.putTask(runnable);
    }
    
  /*  public boolean isConnect(){
       
        if(clientConnector != null && clientConnector.isConnected()){
        	System.out.println("connect true");
            return true;
        }
        return false;
    }
    */
      
    public void requestData(String type){
        clientConnector.requestData(type);
    }
   
    class ThreadPool implements Runnable{
        private Thread _mainThread;
        private Vector _taskQueue;
        private boolean _running;

        public ThreadPool(){
            this._taskQueue = new Vector();
            this._running = true;
            start();

        }

        public void start(){
            this._mainThread = new Thread(this);
            this._mainThread.start();        	
        }
        
        public void reset(){
//        	removeTask();
        	this._mainThread = null;
        }
        
        public void run() {
            Runnable task;
            while(_running){
                try{
                    //Process task here
                    task = getTask();
                    if(task != null){
                        task.run();
                    }else{
                        _taskQueue.removeElement(task);
                    }
                }catch(Exception ex){
    //                System.out.println("Main thread pool error:" + ex.toString());
                }
            }
        }


        public void resetTask(Runnable task){
            try{
                synchronized(_taskQueue){
                    if(_taskQueue != null){
                        _taskQueue.remove(task);
                    }
                }
            }catch(Exception ex){
                System.out.println("Error putTask:" + ex.toString());
            }
        }
        
        public void putTask(Runnable task){
            try{
                synchronized(_taskQueue){
                    if(_taskQueue != null){
                        _taskQueue.addElement(task);
                    }
                }
            }catch(Exception ex){
                System.out.println("Error putTask:" + ex.toString());
            }
        }

        public Runnable getTask(){
            Runnable task = null;
            try{
                synchronized(_taskQueue){
                    if(_taskQueue.size() > 0){
                        task = (Runnable)this._taskQueue.elementAt(0);
                        this._taskQueue.removeElementAt(0);
                        putTask(task);
                    }
                }
            }catch(Exception ex){
                System.out.println("Error getTask:" + ex.toString());
            }
            return task;
        }
    }

	
}
